﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace SuporeSliplampApp.Util
{
    public class WindowOperator
    {
        //blic static int index = -1;
        //blic static DependencyObject parent = null;
        public static void AddToParent(UIElement child, DependencyObject parent, int index)
        {
            if (parent == null)
                return;
            if (parent is ItemsControl itemsControl)
            {
                if (index == -1)
                    itemsControl.Items.Add(child);
                else
                    itemsControl.Items.Insert(index, child);
            }
            else if (parent is Panel panel)
            {
                if (index == -1)
                    panel.Children.Add(child);
                else
                    panel.Children.Insert(index, child);
            }
            else if (parent is Decorator decorator)

                decorator.Child = child;

            else if (parent is ContentPresenter contentPresenter)

                contentPresenter.Content = child;

            else if (parent is ContentControl contentControl)

                contentControl.Content = child;
        }

        public static bool RemoveFromParent( UIElement child, out DependencyObject parent, out int  index)
        {
            parent = VisualTreeHelper.GetParent(child);
            if (parent == null)
            {
                index = -1;
                return false;
            }          
            if (parent is ItemsControl itemsControl)
            {
                if (itemsControl.Items.Contains(child))
                {
                    index = itemsControl.Items.IndexOf(child);
                    itemsControl.Items.Remove(child);
                    return false;
                }
            }
            else if (parent is Panel panel)
            {
                if (panel.Children.Contains(child))
                {
                    index = panel.Children.IndexOf(child);
                    panel.Children.Remove(child);
                    return true;
                }
            }
            else if (parent is Decorator decorator)
            {
                if (decorator.Child == child)
                {
                    decorator.Child = null;
                    index = -1;
                    return true;
                }
            }
            else if (parent is ContentPresenter presenter)
            {
                if (presenter.Content == child)
                {
                    presenter.Content = null;
                    index = -1;
                    return true;
                }
            }
            else if (parent is ContentControl contentControl)
            {
                if (contentControl.Content == child)
                {
                    contentControl.Content = null;
                    index = -1;
                    return true;
                }
            }
            index = -1;
            return false;     
        }

        public static DependencyObject GetParent(DependencyObject depObj, bool isVisualTree)
        {
            if (isVisualTree)
            {
                if(depObj is Visual || depObj is Visual3D)
                    return VisualTreeHelper.GetParent(depObj);
                return null;
            }
            else
                return LogicalTreeHelper.GetParent(depObj);
        }

        public static List<T> FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            try 
            {
                List<T>TList = new List<T>();
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is T)
                    {
                        TList.Add((T)child);
                        List<T> childOfChildren = FindVisualChild<T>(child);
                        if (childOfChildren != null)
                        {
                            TList.AddRange(childOfChildren);
                        }
                    }
                    else
                    {
                        List<T> childOfChildren = FindVisualChild<T>(child);
                        if (childOfChildren != null)
                        {
                            TList.AddRange(childOfChildren);
                        }
                    }
                }
                return TList;
            }
            catch (Exception ex) 
            { 
                return null;
            }
        }

        public static List<T> GetChildObjects<T>(DependencyObject parentObj, Type typename) where T : FrameworkElement
        {
            DependencyObject child = null;
            List<T> childList = new List<T>();

            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(parentObj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(parentObj, i);

                if (child is T && (((T)child).GetType() == typename))
                {
                    childList.Add((T)child);
                }
                childList.AddRange(GetChildObjects<T>(child, typename));
            }
            return childList;
        }
    }
}
